package com.example.leavemanagement.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.leavemanagement.annotation.AuthCheck;
import com.example.leavemanagement.common.BaseResponse;
import com.example.leavemanagement.common.DeleteRequest;
import com.example.leavemanagement.common.ResultUtils;
import com.example.leavemanagement.constant.UserConstant;
import com.example.leavemanagement.exception.ErrorCode;
import com.example.leavemanagement.exception.ThrowUtils;
import com.example.leavemanagement.model.dto.score.AddOrDecreaseScoreRequest;
import com.example.leavemanagement.model.dto.score.QueryScoreRequest;
import com.example.leavemanagement.model.dto.score.UpdateScoreRequest;
import com.example.leavemanagement.model.entity.ScoreManage;
import com.example.leavemanagement.model.vo.Analyze.UserScoreAnalyzeVo;
import com.example.leavemanagement.model.vo.ScoreManageVo;
import com.example.leavemanagement.service.ScoreManageService;
import com.example.leavemanagement.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author 黄宽亮
 * @version 1.0
 * @description: TODO
 * @date 2025/4/30 08:44
 */
@RestController
@RequestMapping("/score")
@Slf4j
public class ScoreController {
    @Resource
    ScoreManageService scoreManageService;
    @Autowired
    private UserServiceImpl userServiceImpl;

    /**
     * 增加或减少积分（管理员）
     * @param addOrDecreaseScoreRequest
     * @param request
     * @return
     */
    @PostMapping("/addScore")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addOrDecreaseScore(@RequestBody AddOrDecreaseScoreRequest addOrDecreaseScoreRequest, HttpServletRequest request) {
        return ResultUtils.success(scoreManageService.addScore(addOrDecreaseScoreRequest,request));
    }

    /**
     * 更新积分（管理员）
     * @param updateScoreRequest
     * @param request
     * @return
     */
    @PostMapping("/updateScore")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateScore(@RequestBody UpdateScoreRequest updateScoreRequest,HttpServletRequest request) {
        return ResultUtils.success(scoreManageService.updateScore(updateScoreRequest,request));
    }

    /**
     * 分页查询积分(管理员）
     * @param queryScoreRequest
     * @param request
     * @return
     */
    @PostMapping("/getScoreByPage")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<ScoreManageVo>> getScoreByPage(@RequestBody QueryScoreRequest queryScoreRequest,HttpServletRequest request) {
        return ResultUtils.success(scoreManageService.getScoreByPage(queryScoreRequest,request));
    }

    /**
     * 删除积分记录（管理员）
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/deleteScore")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteScore(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(ObjectUtil.isNull(deleteRequest), ErrorCode.PARAMS_ERROR);
        ScoreManage oneScore = scoreManageService.getById(deleteRequest.getId());
        ThrowUtils.throwIf(ObjectUtil.isNull(oneScore), ErrorCode.NOT_FOUND_ERROR,"该记录不存在");
        boolean result = scoreManageService.deleteScoreById(oneScore, request);
        return result? ResultUtils.success(true) : (BaseResponse<Boolean>) ResultUtils.error(400,"删除失败");
    }


    /**
     * 根据id查询积分记录（用户）
     *
     * @param queryScoreRequest
     * @param request
     * @return
     */
    @PostMapping("/getUserScoreAnalyzeVo")
    @AuthCheck(mustRole = UserConstant.USER_LOGIN_STATE)
    public Page<UserScoreAnalyzeVo> getUserScoreAnalyzeVo(@RequestBody QueryScoreRequest queryScoreRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(ObjectUtil.isNull(queryScoreRequest), ErrorCode.PARAMS_ERROR);
        Page<UserScoreAnalyzeVo> userScoreAnalyzeVoByPage = scoreManageService.getUserScoreAnalyzeVoByPage(queryScoreRequest, request);
        return userScoreAnalyzeVoByPage;
    }
}
